# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class Scrapy51JobPipeline:
    def __init__(self):
        self.db = pymysql.connect("localhost", "root", "Kernel990621", "job51")
        self.cursor = self.db.cursor()

    def process_item(self, item, spider):
        sql = "INSERT INTO 51job(`job_name`, `company_name`, `release_date`, `location`, `salary_min`, `salary_max`," \
              " `job_url`, `academic_requirements`, `company_nature`, `industry`, `recruits_number`, " \
              "`employee_welfare`, `job_description`) " \
              "VALUES('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}')".format(
            item["job_name"], item["company_name"], item["release_date"], item["location"], item["salary_min"],
            item["salary_max"], item["job_url"], item["academic_requirements"], item["company_nature"],
            item["industry"], item["recruits_number"], item["employee_welfare"], item["job_description"].strip())
        try:
            self.cursor.execute(sql)
            self.db.commit()
        except Exception:
            self.db.rollback()
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.db.close()
